package fgame
{
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	
	import fgame.controller.Controller;
	import fgame.events.MapEvent;
	import fgame.scene.SceneManager;
	
	import flare.basic.Scene3D;
	import flare.core.Pivot3D;
	import flare.materials.filters.AlphaMaskFilter;
	import flare.materials.filters.ColorMatrixFilter;
	import flare.materials.filters.ColorTransformFilter;
	import flare.materials.filters.FogFilter;
	import flare.materials.filters.VertexColorFilter;
	
	import net.WorkQueue;
	
	import ricky.utils.FrameManager;
	import ui.UIManager;

	public class GameState
	{
		public function GameState()
		{
			_sceneManager=new SceneManager();
			_sceneManager.addEventListener(MapEvent.MAP_LOADED,onSceneLoaded);
			_sceneManager.addEventListener(MapEvent.MOUSE_CLICK,mapClick);
		}
		
		//////////////////////////////////////////////////////////////////////////
		//****************** public ******************************
		//////////////////////////////////////////////////////////////////////////

		public function get viewer3D():Pivot3D
		{
			return _viewer3D;
		}

		/**
		 * 初始化调用
		 */
		public function init(stage:Sprite):void
		{
			//初始化样式
//			StyleManager.getInstance().loadCss([new ThemeRegister()]);

			this.root=stage;
			//设置舞台
			this._stage=stage.stage;
			FrameManager.inst.stage=_stage;

			//初始化flare3d场景
			initScene();
		}
		
		//////////////////////////////////////////////////////////////////////////
		//*************************** private ***********************************
		//////////////////////////////////////////////////////////////////////////
		
		/**
		 * 游戏场景加载完成
		 */
		private function onSceneLoaded(e:MapEvent):void
		{
			_sceneManager.scene.parent=scene;
//			this.scene.addChild(_sceneManager.scene);
//			if(_sceneManager.cameraRoot)
//				this.scene.camera=_cameraRoot
			UIManager.inst.removeLoading();
		}
		
		private function mapClick(e:MapEvent):void
		{
			Controller.instance.mapClick(e.info.point);
		}
		
		/**
		 * 初始化flare3d场景
		 */
		private function initScene():void
		{

			scene=new Scene3D(this.stage);
			scene.showLogo=false;
			var width:Number=this.stage.stageWidth;
			var height:Number=this.stage.stageHeight;
			scene.camera.setPosition( 0, 50, -width/2 );
//			scene.camera.lookAt( 0, 55, -width/2  );
//			scene.camera.setRotation(0,0,0);
			scene.registerClass(ColorMatrixFilter);
			scene.registerClass(AlphaMaskFilter);
			scene.registerClass(FogFilter);
			scene.registerClass(ColorTransformFilter);
			scene.registerClass(VertexColorFilter);
			
			_viewer3D=new Pivot3D();
			scene.addChild(_viewer3D);
			_viewer3D.x=0;//width/3;

		}
		
		/**
		 * 游戏主循环
		 */
		private function updateEvent(e:Event):void
		{
			Controller.instance.updateEvent(e);
			
			//消息队列
			WorkQueue.instance.onProccessQueue(e);
			
			//act
			FrameManager.inst.update(e);
		
		}
		
		///////////////////////////////////////////////////////////////////////////
		//****************************公共属性*************************************
		///////////////////////////////////////////////////////////////////////////
		
		/**
		 * 根场景
		 */
		public function get root():DisplayObjectContainer
		{
			return _root;
		}
		
		public function set root(value:DisplayObjectContainer):void
		{
			_root = value;
		}
		
		/**
		 * 游戏是否运行
		 */
		public function get running():Boolean
		{
			return _isRunning;
		}
		
		public function set running(value:Boolean):void
		{
			_isRunning = value;
			if(_isRunning)
			{
				this.scene.removeEventListener(Scene3D.UPDATE_EVENT,updateEvent);
				this.scene.addEventListener(Scene3D.UPDATE_EVENT,updateEvent);
			}else
				this.scene.removeEventListener(Scene3D.UPDATE_EVENT,updateEvent);
		}

		/**
		 * flare3d场景
		 */
		public function get scene():Scene3D
		{
			return _scene;
		}

		public function set scene(value:Scene3D):void
		{
			_scene = value;
		}

		public function get stage():Stage
		{
			return _stage;
		}
		
		public function get sceneManager():SceneManager
		{
			return _sceneManager;
		}
		
		//
		private var _stage:Stage;
		private var _sceneManager:SceneManager;
		private var _isRunning:Boolean=false;
		private var _scene:Scene3D; 
		private var _root:DisplayObjectContainer;
		private var _viewer3D:Pivot3D;
		
		private static var _inst:GameState;
		
		public static function get inst():GameState
		{
			if(!_inst)
				_inst=new GameState();
			return _inst;
		}
	}
}